// SaveAsWebApi.cs
// <copyright>Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <summary>This file contains code that creates web pages from pages in
// a Visio document.</summary>

using System;

namespace Scenario.Modeler
{

	/// <summary>This class is used to create web pages from pages in a Visio
	/// document using the SaveAsWeb add-on.</summary>
	public sealed class SaveAsWebApi 
	{

		/// <summary>This constant field contains the value to be or'ed into a
		/// flag value if SaveDocAsWebPage should display the left pane on the
		/// web page.</summary>
		public const int ShowPropertiesWindow = 1;

		/// <summary>This constant field contains the value to be or'ed into a
		/// flag value if SaveDocAsWebPage should display the Go To Page tool in
		/// the left pane on the web page.</summary>
		public const int ShowNavigationBar = 2;

		/// <summary>This constant field contains the value to be be or'ed into
		/// a flag value if SaveDocAsWebPage should display the Search tool in
		/// the left pane on the web page.</summary>
		public const int ShowSearchTool = 4;

		/// <summary>This constant field contains the value to be or'ed into a
		/// flag value if SaveDocAsWebPage should display the Pan and Zoom tool
		/// in the left pane on the web page.</summary>
		public const int ShowPanAndZoom = 8;

		/// <summary>This constant field contains the value to be or'ed into a
		/// flag value if SaveDocAsWebPage should be run without displaying any
		/// user interface dialogs or browser windows.</summary>
		public const int RunInBatchMode = 16;

		/// <summary>This constructor is intentionally left blank.</summary>
		private SaveAsWebApi() 
		{

			// No initialization is required.
		}

		/// <summary>This method saves pages in the specified document to a web
		/// page.</summary>
		/// <remarks>This code uses the SaveAsWeb Visio add-on to create the web
		/// pages.  In Visio 11 the SaveAsWeb interface is available as a
		/// property on the Visio Application object.  The various settings are
		/// set on the SaveAsWeb object's WebPageSettings</remarks>
		/// <param name="targetDocument">Document to save as a web page</param>
		/// <param name="startPage">First page number to save (Set both
		/// startPage and endPage to -1 for all pages.)</param>
		/// <param name="endPage">Last page number to save (Set both startPage
		/// and endPage to -1 for all pages.)</param>
		/// <param name="webFileName">Full path name of the web file to create
		/// (for example, @"c:\webpages\visiodoc.html")</param>
		/// <param name="flags">Combination of constants from the
		/// SaveAsWebApi class, or'ed together to modify the new web page and
		/// run-time behavior</param>
		/// <returns>True if the web page is created successfully</returns>
		public static bool SaveDocAsWebPage(
			Microsoft.Office.Interop.Visio.Document targetDocument,
			int startPage,
			int endPage,
			string webFileName,
			int flags) 
		{

			bool webPageCreated = false;
			Microsoft.Office.Interop.Visio.
				Application targetApplication = null;
			Microsoft.Office.Interop.Visio.
				SaveAsWeb.VisSaveAsWeb saveAsWebAddon = null;
			Microsoft.Office.Interop.Visio.
				SaveAsWeb.VisWebPageSettings saveAsWebSetting = null;

			try 
			{

				// Get the application containing the document.
				targetApplication = targetDocument.Application;

				// Get the SaveAsWeb object from the application
				saveAsWebAddon = 
					(Microsoft.Office.Interop.Visio.SaveAsWeb.
					VisSaveAsWeb)targetApplication.SaveAsWebObject;



				// Get the SaveAsWeb settings object from the add-on.  
				// SaveAsWeb settings are set on this object.
				saveAsWebSetting = 
					(Microsoft.Office.Interop.Visio.SaveAsWeb.
					VisWebPageSettings)saveAsWebAddon.WebPageSettings;

				// Validate that the page numbers are valid.
				if ((startPage == -1) && (endPage == -1)) 
				{

					// These are valid values indicating that all pages
					// will be included.
				}
				else if (startPage > endPage) 
				{

					// The start page is greater than the end page.
					throw new ArgumentOutOfRangeException("startPage",
						"The start page must be less than or equal to " +
						"the end page.");					
				}
				else if (startPage > targetDocument.Pages.Count ||
					endPage > targetDocument.Pages.Count) 
				{
					
					// The range of pages is invalid.
					throw new ArgumentOutOfRangeException("startPage or " +
						"endPage", "The start and end pages must both be " +
						"less than or equal to the number of pages in the " +
						"Visio document.");
				}
				else 
				{
					saveAsWebSetting.StartPage = startPage;
					saveAsWebSetting.EndPage = endPage;
				}			

				// Use AttachToVisioDoc to indicate which document to save 
				// as a Web page.
				saveAsWebAddon.AttachToVisioDoc(targetDocument);			

				// Set the add-on's settings

				// Add the range of pages to include in the web page.
				saveAsWebSetting.StartPage = startPage;
				saveAsWebSetting.EndPage = endPage;

				// Add the name of the HTML file to be created.			
				saveAsWebSetting.TargetPath = webFileName;

				// Add the information about whether there is a left
				// pane in the browser window.
				saveAsWebSetting.PropControl = 
					(flags & ShowPropertiesWindow) != 0 ? 1 : 0;

				// Add the information about whether there is a search
				// tool in the left pane of the browser window.
				saveAsWebSetting.Search = 
					(flags & ShowSearchTool) != 0 ? 1 : 0;

				// Add the information about whether there is a Pan 
				// and Zoom tool in the left pane of the browser window.
				saveAsWebSetting.PanAndZoom = 
					(flags & ShowPanAndZoom) != 0 ? 1 : 0;

				// Add the information about whether there is a 
				// Go To Page tool in the left pane of the browser window.
				saveAsWebSetting.NavBar = 
					(flags & ShowNavigationBar) != 0 ? 1 : 0;
				
				// Add the information about running in batch mode.
				if ((flags & RunInBatchMode) != 0) 
				{

					// If in batch mode, the browser window isn't opened
					// and the silent mode is turned on to suppress all
					// dialogs from Visio and the browser.
					saveAsWebSetting.OpenBrowser = 0;
					saveAsWebSetting.SilentMode = 1;
				}
				else 
				{
					
					// If not in batch mode, the browser window is
					// opened and the quiet mode is turned on to suppress
					// all dialogs from Visio.
					saveAsWebSetting.OpenBrowser = 1;
					saveAsWebSetting.QuietMode = 1;
				}				

				// Run the add-on to create the web page and related files.
				saveAsWebAddon.CreatePages();

				// The web page was created successfully.
				webPageCreated = true;
			}

			catch (Exception error) 
			{
				System.Diagnostics.Debug.WriteLine(error.Message);
			}

			return webPageCreated;
		}		
	}
}

